// index.js
Page({
  data: {
    timeStart: '',
    timeEnd: (new Date().getFullYear() + 1)+'-'+ '12',
    array: ['男职工', '原法定退休年龄55周岁女职工', '原法定退休年龄50周岁女职工'],
    index: null,
    date: '',
    lifeYear: 0,
    liftMonth: 0,
    Month: 0,
    lifeTimeYear: 0,
    lifeTimeMonth: 0,
    shebaoYear: 15,
    shebaoMonth: 0
  },
  formReset: function(){
    this.setData({
      index: null,
      date: '',
      lifeYear: 0
    })
  },
  formSubmit: function(e){
    const type = this.data.index
    if(!type){
      return
    }
    const baseNumber =  type === '0' ? 60 : (type === '1' ? 55 : 50)
    const baseYear =  type === '0' ? 1965 : (type === '1' ? 1970 : 1975)
    const bigYear =   type === '0' ? 63 : (type === '1' ? 58 : 55)
    const cileMonth = type === '2' ? 2 : 4
    const cileYear =  type === '2' ? 6 : 3
    const d = this.data.date.split('-')
    const y = Number(d[0])
    const m = Number(d[1])
    const timeYear = y - baseYear
    const timeMonth = Math.ceil(m / cileMonth)
    const totalM = timeYear * cileYear + timeMonth
    const year = baseNumber + Math.floor(totalM / 12)
    const month = totalM % 12
    let ly1 = year
    let lm1 = month
    let m1 = totalM
    console.log(baseNumber,baseYear,bigYear)
    // bigYear最大退休年龄
    if(year >= bigYear){
      ly1 = bigYear // 退休年龄
      lm1 = 0  // 退休年龄-月
      m1 = 36  // 延迟月数
    }
    let y1 = y +ly1 // 退休时间 - 年
    let m2 = lm1 + m // 退休时间 - 月
    let y1s = m2 > 12 ? y1 + 1 : y1
    let shebaoYears = y1s <= 2029 ? 15 : 15 + Math.floor((y1s - 2029)/2)
    let shebaoMonths = y1s <= 2029 ? 0 : ((y1s - 2029)%2 ? 6 : 0)
    this.setData({
      lifeYear: ly1,
      liftMonth: lm1,
      Month: m1,
      lifeTimeYear: y1s,
      lifeTimeMonth: m2 > 12 ? m2 % 12 : m2,
      shebaoYear: shebaoYears >= 20 ? 20 : shebaoYears,
      shebaoMonth: shebaoYears >= 20 ? 0 : shebaoMonths
    })
  },
  bindPickerChange: function(e) {
    const type = e.detail.value
    this.setData({
      index: e.detail.value,
      date: null,
      lifeYear: 0,
      timeStart: type === '0' ? '1965-01' : (type === '1' ? '1970-01' : '1975-01')
    })
    console.log(type, this.data.timeStart)
  },
  bindDateChange: function(e) {
    this.setData({
      lifeYear: 0,
      date: e.detail.value
    })
  },
  
  onLoad: function (options) {
    // 获取启动参数
    var scene = wx.getLaunchOptionsSync().scene
    console.log('scene: ', scene)
    if (scene === 1044) {
      // 从微信群中分享进入
      wx.getShareInfo({
        shareTicket: options.shareTicket,
        success: function (res) {
          console.log(res)
          // 解密分享信息
          var encryptedData = res.encryptedData
          var iv = res.iv
          console.log('encryptedData: ', encryptedData)
          console.log('iv: ', iv)
        },
        fail: function (res) {
          console.log(res)
        }
      })
    }
    wx.showShareMenu({

      withShareTicket:true,

      menus:['shareAppMessage','shareTimeline']

      })
  },
 
  // onShareTap: function () {
  //   // 显示分享按钮
  //   wx.showShareMenu({
  //     withShareTicket: true,
  //     menus:['shareAppMessage','shareTimeline']
  //   })
  // },
 
  onShareAppMessage: function () {
    return {
      title: '退休年龄测试',
      path: '/pages/index/index',
      imageUrl: '',
      success: function (res) {
        // 分享成功
        console.log(res)
      },
      fail: function (res) {
        // 分享失败
        console.log(res)
      }
    }
  }
})
